<?php
require_once "database/Database.php";


class DatabaseUpdate
{
	public static function updateTable($dbTableInstance)
	{
		$dbName = $dbTableInstance->getDbName();
		$dbTableName = $dbTableInstance->getDbTableName();
		$dbPriKey =  $dbTableInstance->getDbPrimaryKey();
		 
		if($dbName == NULL || $dbTableName == NULL ||  $dbPriKey == null)
		{
			throw new Exception("Invalid parameter for Database::updateTableByCondition!");
		}
	
		//Get class property
		$reflect =new ReflectionClass($dbTableInstance);
		$props   = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
		$propSize = count($props);
		 
		if($propSize < 2) //include primary key
		{
			throw new Exception("No property is found for dbTableInstance.");
		}
		 
		//generate sql.
		$sql = "UPDATE " . $dbName . "." . $dbTableName . " SET ";
		$bindArray = array();
		$priProp = NULL;
		 
		foreach ($props as $prop)
		{
			if($priProp == null && $prop->getName() == $dbPriKey)
			{
				$priProp = $prop;
				continue;
			}
	
			if($prop->getValue($dbTableInstance) === null)//null keep unchanged and empty means clear.
			{
				continue;
			}
	
			$bindArray[] = $prop;
			if(count($bindArray) > 1)
			{
				$sql .= ",";
			}
	
			$sql .=  $prop->getName() . "=:" . $prop->getName();
		}
		 
		if($priProp == null)
		{
			throw new Exception("Missing primary key:" . $dbPriKey . " when update table:" . $reflect->getName());
		}
	
		$sql .= " where " . $priProp->getName() . "=:" . $priProp->getName();
	
		 
		$dbConnection  = Database::getConnection();
		$stmt = $dbConnection->prepare($sql);
	
		foreach ($bindArray as $prop)
		{
			$stmt->bindValue(':'.$prop->getName(),$prop->getValue($dbTableInstance));
		}
		$stmt->bindValue(':'.$priProp->getName(),$priProp->getValue($dbTableInstance));
		 
		if(false === $stmt->execute())
		{
			$message = $sql . '<br/>' . implode ('<br/>',$stmt->errorInfo());
			throw new Exception($message);
		}
	}
	
	public static function insertTable($dbTableInstance)
	{
		$dbName = $dbTableInstance->getDbName();
		$dbTableName = $dbTableInstance->getDbTableName();
		if($dbName == NULL || $dbTableName == NULL)
		{
			throw new Exception("Invalid parameter for Database::insertTable!");
		}
		 
		//Get class property
		$reflect=new ReflectionClass($dbTableInstance);
		$props   = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
		$propSize = count($props);
		if($propSize < 1)
		{
			throw new Exception("No property is found for dbTableInstance.");
		}
		 
		///generate sql string.
		$fieldsString = $props[0]->getName();
		$parametersString = ":" . $props[0]->getName();
		for($i = 1; $i < $propSize ; $i ++)
		{
		$fieldsString .= "," . $props[$i]->getName();
		$parametersString .= ",:".$props[$i]->getName();
		}
			
		$sql="INSERT INTO " . $dbName . "." . $dbTableName;
		$sql .= " (" . $fieldsString . ")" ;
		$sql .= " values(" . $parametersString . ");";
	
		$dbConnection  = Database::getConnection();
		$stmt = $dbConnection->prepare($sql);
	
		foreach ($props as $prop)
		{
		$stmt->bindValue(':'.$prop->getName(),$prop->getValue($dbTableInstance));
		}
		 
		if(false === $stmt->execute())
		{
		$message = $sql . '<br/>' . implode ('<br/>',$stmt->errorInfo());
				throw new Exception($message);
		}
	}
}